package com.dz.Incubator.controller;

import com.dz.Incubator.config.Pagination;
import com.dz.Incubator.config.ReturnCode;
import com.dz.Incubator.dto.BindPhoneDto;
import com.dz.Incubator.entity.InvokeResult;
import com.dz.Incubator.service.SysUserService;
import com.dz.Incubator.entity.SysUserEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/api/a/zx/sysUser")
@Slf4j
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * @api {GET} /a/zx/sysUser/sendRandCode
     * 发送短信验证码
     * @apiName sendRandCode
     * @apiGroup user
     * @apiVersion 1.0.0
     * @apiParam {String}         mobile          手机号
     * @apiExample 使用示例:路径
     * /a/member/user/sendRandCode
     * @apiSuccess {String}      code
     * @apiSuccess {String}      msg                        成功失败具体说明
     * @apiSuccess {Object}      data                       返回数据
     * @apiSuccess {int}     data.id             用户ID
     * @apiSuccessExample {json} 成功:
     * {
     * "code":"0000",
     * "msg":"请求成功!",
     * "data":{
     * "id": 10
     * }}
     */
//    @RequestMapping(value = "/sendRandCode", method = RequestMethod.GET)
//    public InvokeResult sendRandCode(@RequestParam String mobile) {
//        InvokeResult invokeResult;
//        try {
//            if (null == mobile) {
//                invokeResult = InvokeResult.failure("参数错误！");
//                return invokeResult;
//            }
//            Cache.setPhoneRandomCode(mobile, "123456");
//            invokeResult = InvokeResult.success();
//        } catch (Exception e) {
//            invokeResult = InvokeResult.failure("短信发送失败！");
//        }
//        return invokeResult;
//    }

    /**
     * 分页查询 sysUser 对象
     *
     */
    @RequestMapping(value = "/getPageList", method = RequestMethod.POST)
    @ResponseBody
    public InvokeResult<Pagination> getPageList(@RequestBody Pagination pagination) {
        InvokeResult invokeResult = null;
        try {
            if (null == pagination) {
                return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
            Pagination pageInfo = sysUserService.findByPagination(pagination);
            if (null != pageInfo && null != pageInfo.getList() && pageInfo.getList().size() > 0) {
                invokeResult = InvokeResult.success(pageInfo);
            } else {
                invokeResult = InvokeResult.pass_no_data();
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 获取单个SysUserEntity 对象
     *
     * @param id Id
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public InvokeResult<SysUserEntity> get(@PathVariable Long id) {
        InvokeResult invokeResult = null;
        try {
            if (null != id) {
                SysUserEntity sysUserEntity = sysUserService.get(id);
                if (null == sysUserEntity) {
                    invokeResult = InvokeResult.pass_no_data();
                }
                invokeResult = InvokeResult.success(sysUserEntity);
            } else {
                return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 删除单个sysUserEntity 对象
     *
     * @param id 删除数据 ID
     */
    @RequestMapping(value = "/del", method = RequestMethod.GET)
    @ResponseBody
    public InvokeResult delete(@RequestParam Long id) {
        InvokeResult invokeResult = null;
        try {
            if (null != id) {
                sysUserService.delete(id);
                invokeResult = InvokeResult.success();
            } else {
                return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 集合查找 sysUserEntity 对象
     *
     */
    @RequestMapping(value = "/find", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public InvokeResult<List<SysUserEntity>> find(@RequestParam("ids") List<Long> ids) {
        InvokeResult invokeResult = null;
        try {
            if (null != ids && ids.size() > 0) {
                Long[] skuIds = ids.toArray(new Long[ids.size()]);
                List<SysUserEntity> sysUserEntitys = sysUserService.find(skuIds);
                if (null != sysUserEntitys && sysUserEntitys.size() > 0) {
                    invokeResult = InvokeResult.success(sysUserEntitys);
                } else {
                    invokeResult = InvokeResult.pass_no_data();
                }
            } else {
                return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 查找所有 sysUserEntity 对象
     */
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    @ResponseBody
    public InvokeResult<List<SysUserEntity>> findAll() {
        InvokeResult invokeResult = null;
        try {
            List<SysUserEntity> sysUserEntitys = sysUserService.findAll();
            if (null != sysUserEntitys && sysUserEntitys.size() > 0) {
                invokeResult = InvokeResult.success(sysUserEntitys);
            } else {
                invokeResult = InvokeResult.pass_no_data();
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 新增数据
     *
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public InvokeResult<SysUserEntity> add(@RequestBody SysUserEntity sysUserEntity) {
        InvokeResult invokeResult = null;
        try {
            if (null != sysUserEntity) {
                invokeResult = InvokeResult.success(sysUserService.insert(sysUserEntity));
            } else {
                return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 修改数据
     *
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public InvokeResult<SysUserEntity> update(@RequestBody SysUserEntity sysUserEntity) {
        InvokeResult invokeResult = null;
        try {
            if (null != sysUserEntity && null != sysUserEntity.getId()) {
                invokeResult = InvokeResult.success(sysUserService.update(sysUserEntity));
            } else {
                return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    @RequestMapping(value = "bindPhone", method = RequestMethod.POST)
    @ResponseBody
    public InvokeResult bindPhone(@RequestBody BindPhoneDto dto) {
        sysUserService.bindPhone(dto.getId(), dto.getPhoneNumber());
        return InvokeResult.success();
    }

}